package com.laiqilin.controller;

import com.laiqilin.common.util.Result;
import com.laiqilin.entity.Shelf;
import com.laiqilin.service.IShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/shelf")
public class ShelfController {

	@Autowired
	private IShelfService shelfService;

	// 获取货架列表
	@RequestMapping("/list")
	public Result getShelf() {
		return Result.ok().data("list", shelfService.list());
	}

	// 添加
	@PostMapping("/add")
	public Result add(@RequestBody Shelf shelf) {
		boolean flag = shelfService.save(shelf);
		return flag ? Result.ok().message("添加成功!") : Result.error().message("添加失败!");
	}

	// 修改
	@PutMapping("/update")
	public Result update(@RequestBody Shelf shelf) {
		boolean flag = shelfService.updateById(shelf);
		return flag ? Result.ok().message("修改成功!") : Result.error().message("修改失败!");
	}

	// 批量删除
	@DeleteMapping("/delete/{ids}")
	public Result delete(@PathVariable("ids") String ids) {
		String[] temp = ids.split(",");
		List<String> list = Arrays.asList(temp);
		return shelfService.removeByIds(list) ? Result.ok().message("删除成功!") : Result.error().message("删除失败!");
	}
}
